<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>心理答疑管理</el-breadcrumb-item>
			<el-breadcrumb-item>回复管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="回复名称">
					<el-input v-model="queryInfo.questionContent" @clear="queryList" placeholder="请输
入回复内容"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="toAddReply">新增回复</el-button>
					<!-- <el-button type="danger">批量删除</el-button> -->
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 表格区域 -->
		<el-table :data="replyList" border style="width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="replyId" label="ID"></el-table-column>
			<el-table-column prop="replyContent" label="回复内容">
			</el-table-column>
			<el-table-column prop="consultId" label="咨询师">
				<template slot-scope="scope">
					{{consults[scope.row.consultId]}}
				</template>
			</el-table-column>
			<el-table-column prop="questionId" label="回复问题">
				<template slot-scope="scope">
					{{questions[scope.row.questionId]}}
				</template>
			</el-table-column>
			</el-table-column>
			<el-table-column prop="replyTime" label="回复时间">
			</el-table-column>
			<el-table-column label="操作">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toDeleteReply(scope.row.replyId)">删除回复</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>


		<!-- 新增回复的对话框 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="reply">
				<el-form-item label="回复题目" label-width="80px">
					<template>
					  <el-select v-model="reply.questionId" filterable placeholder="请选择">
					    <el-option
					      v-for="item in questionList"
					      :key="item.questionId"
					      :label="item.questionTitle"
					      :value="item.questionId">
					    </el-option>
					  </el-select>
					</template>
					<!-- <el-input placeholder="请输入课程类型" v-model="course.courseTypeId"></el-input> -->
				</el-form-item>
				<el-form-item label="回复内容" label-width="80px">
					<el-input placeholder="请输入回复内容" v-model="reply.questionContent"></el-input>
				</el-form-item>
				<el-form-item label="回复讲师" label-width="80px">
					<template>
					  <el-select v-model="reply.consultId" filterable placeholder="请选择">
					    <el-option
					      v-for="item in consultList"
					      :key="item.consultId"
					      :label="item.consultName"
					      :value="item.consultId">
					    </el-option>
					  </el-select>
					</template>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="addReply">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		url
	} from '../../util/tool.js'
	export default {
		data() {
			return {

				url: '', // 定义一个全局的url
				// 查询信息对象
				queryInfo: {
					questionContent: '',
					pageNum: 1,
					pageSize: 10
				},
				total: 0,
				replyList: [],
				title: '',
				dialogFormVisible: false,
				reply: { // 回复对象
					consultId: '',
					replyId: '',
					questionContent: '',
					questionId:''
				},
				dialogImageUrl: '', // 预览图片路径
				dialogVisible: false, // 预览图片的对话框
				questions:[],
				questionList:[],
				consults:[],
				consultList:[]
			}
		},
		methods: {
			// 查询所有问题信息
			queryQuestion(){
				this.$http.get('question/findAll').then(res=>{
					this.questionList = res.data.data
					for(var i=0;i<res.data.data.length;i++){
						this.$set(this.questions, res.data.data[i].questionId,res.data.data[i].questionTitle);
						}
				})
			},
			// 查询所有讲师的信息
			queryConsult(){
				this.$http.get('consult/findAllConsult').then(res=>{
					this.consultList = res.data.data
					for(var i=0;i<res.data.data.length;i++){
						this.$set(this.consults, res.data.data[i].consultId,res.data.data[i].consultName);
						}
				})
			},
			// 处理上传成功的函数
			handleSuccess(response) {
				// 把上传后的图片路径，交给回复对象的coursePicture
				this.reply.coursePicture = response.message;
			},

			// 删除图片的事件
			handleRemove(file, fileList) {
				console.log(file, fileList);
				// 移除数据
				this.reply.coursePicture = '';
			},

			// 预览图片事件
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url;
				this.dialogVisible = true;
			},
			cancle() {
				this.reply = { // 回复对象
					collectionVolume: 0,
					consultId: '',
					courseCount: '',
					replyId: '',
					courseIntroduce: '',
					courseLearning: '',
					questionContent: '',
					courseTime: '',
					courseTypeId: '',
					people: '',
					readingVolume: 0,
					reviewsCount: 0,
					subscription: '',
					videoPath: ''
				}
				// 关闭对话框
				this.dialogFormVisible = false;
				// 重新查询
				this.queryList()
				//  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
				this.fileList = []
			},
			//	去删除回复内容
			toDeleteReply(replyId) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('reply/' + replyId)
							.then(res => {
								if (res.data.status == 200) {
									this.$message.success("删除成功")
									this.queryList()
								} else {
									this.$message.error("删除失败")
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
				//  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
				this.fileList = []
			},


			//	添加或修改回复内容
			addReply() {
				 //	添加
					this.$http.post("reply", this.reply)
						.then(res => {
							if (res.data.status == 200) {
								this.$message.success("添加成功！")
								// 清空数据
								this.reply = { // 回复对象
									collectionVolume: 0,
									consultId: '',
									courseCount: '',
									replyId: '',
									courseIntroduce: '',
									courseLearning: '',
									questionContent: '',
									courseTime: '',
									courseTypeId: '',
									people: '',
									readingVolume: 0,
									reviewsCount: 0,
									subscription: '',
									videoPath: ''
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error("添加失败！")
							}
						})
				//  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
				this.fileList = []
			},

			// 进入添加的对话框
			toAddReply() {
				this.title = "新增回复";
				this.dialogFormVisible = true;

			},

			// 分页查询所有的数据
			queryList() {
				//	调用后端接口
				this.$http.get('reply/findByPage', {
						params: this.queryInfo
					})
					.then(res => {
						if (res.data.status == 200) {
							console.log(res);
							this.replyList = res.data.data.list
							this.total = res.data.data.total
						} else {

						}
					})
			},
			handleSizeChange(size) {
				// console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size
				this.queryInfo.pageNum = 1
				this.queryList()
			},
			handleCurrentChange(num) {
				// console.log('页码发生改变');
				this.queryInfo.pageNum = num
				this.queryList()
			}
		},
		mounted() {
			this.queryList()
			this.queryQuestion()
			this.queryConsult()
			// 动态的获取tool.js中的url，并赋值给当前组件的全局url
			this.url = url;
		}
	}
</script>
<style>
	/**设置富文本编辑器的最小高度，注意style中不能使用scoped **/
	.ql-container {
		min-height: 200px;
	}

	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}
</style>